package org.speed.mes.base.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.speed.mes.base.domain.bo.BsSupplierBo;
import org.speed.mes.base.domain.vo.BsSupplierVo;
import org.speed.mes.base.domain.BsSupplier;
import org.speed.mes.base.mapper.BsSupplierMapper;
import org.speed.mes.base.service.IBsSupplierService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商Service业务层处理
 *
 * @author speed-mes
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class BsSupplierServiceImpl implements IBsSupplierService {

    private final BsSupplierMapper baseMapper;

    /**
     * 查询供应商
     */
    @Override
    public BsSupplierVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询供应商列表
     */
    @Override
    public TableDataInfo<BsSupplierVo> queryPageList(BsSupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsSupplier> lqw = buildQueryWrapper(bo);
        Page<BsSupplierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询供应商列表
     */
    @Override
    public List<BsSupplierVo> queryList(BsSupplierBo bo) {
        LambdaQueryWrapper<BsSupplier> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsSupplier> buildQueryWrapper(BsSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsSupplier> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BsSupplier::getCode, bo.getCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), BsSupplier::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getShortName()), BsSupplier::getShortName, bo.getShortName());
        lqw.like(StringUtils.isNotBlank(bo.getEnName()), BsSupplier::getEnName, bo.getEnName());
        lqw.eq(StringUtils.isNotBlank(bo.getIntro()), BsSupplier::getIntro, bo.getIntro());
        lqw.eq(StringUtils.isNotBlank(bo.getGrade()), BsSupplier::getGrade, bo.getGrade());
        lqw.eq(bo.getScore() != null, BsSupplier::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BsSupplier::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), BsSupplier::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getTelephone()), BsSupplier::getTelephone, bo.getTelephone());
        lqw.eq(StringUtils.isNotBlank(bo.getCreditCode()), BsSupplier::getCreditCode, bo.getCreditCode());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BsSupplier::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增供应商
     */
    @Override
    public Boolean insertByBo(BsSupplierBo bo) {
        BsSupplier add = MapstructUtils.convert(bo, BsSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商
     */
    @Override
    public Boolean updateByBo(BsSupplierBo bo) {
        BsSupplier update = MapstructUtils.convert(bo, BsSupplier.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsSupplier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除供应商
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
